﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OSEnviron
{
    /// <summary>
    /// 将OpenSEES添加到环境变量
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 添加环境变量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectButton_Click(object sender, EventArgs e)
        {
            //获得所有环境变量
            string path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
            //判断环境变量是否存在
            if(path.Contains("OpenSees"))
            {
                //提示
                MessageBox.Show("OpenSEES Environment Variable Has Existed!", "", 
                    MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            //选择OpenSEES
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "exe(*.exe)|*.exe";
            //选择文件
            if (openFileDialog.ShowDialog() != DialogResult.OK) return;
            //获得文件路径
            var filePath = openFileDialog.FileName;
            //判断是否选择了正确的文件
            if (!filePath.Contains("OpenSees.exe"))
            {
                //提示
                MessageBox.Show("File is not CORRECT!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //在C盘创建文件夹
            string directoryPath = "C:\\OpenSees";
            //文件不存在时创建文件夹
            if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);
            //文件路径
            string targetFilePath = string.Format("{0}\\{1}", directoryPath, Path.GetFileName(filePath));
            //文件复制
            File.Copy(filePath, targetFilePath, true);
            //添加环境变量路径
            path += ";" + directoryPath +  ";";
            //设定环境变量
            Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Machine);
            //设定成功
            MessageBox.Show("OpenSees Environment Variable Added!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
}
